package org.bobdawg.app.MALforAndroid;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MALSqlHelper extends SQLiteOpenHelper {

	private static final String DATABASE_NAME = "MALList.db";
	private static final int DATABASE_VERSION = 3;
	private String createAnimeList;
	private String createMangaList;

	public MALSqlHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		createAnimeList = context.getString(R.string.anime_table_def);
		createMangaList = context.getString(R.string.manga_table_def);
		
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(createAnimeList);
		db.execSQL(createMangaList);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		if (oldVersion == 1){
			db.execSQL(createMangaList);
		}
		if (oldVersion <= 2) {
			db.execSQL("ALTER TABLE animeList ADD COLUMN classification BLOB DEFAULT NULL");
			/*db.execSQL("CREATE TEMPORARY TABLE `animeList_backup` (" +
			  "`id` INTEGER UNSIGNED NOT NULL, " +
			  "`title` CHAR(40) NOT NULL, " +
			  "`type` CHAR(40) NOT NULL, " +
			  "`imageUrl` CHAR(40) NOT NULL, " +
			  "`episodes` INTEGER UNSIGNED NOT NULL, " +
			  "`status` CHAR(40) NOT NULL, " +
			  "`watchedEpisodes` INTEGER UNSIGNED NOT NULL, " +
			  "`score` INTEGER UNSIGNED NOT NULL, " +
			  "`watchedStatus` CHAR(40) NOT NULL, " +
			  "`dirty` INTEGER UNSIGNED NOT NULL DEFAULT 0, " +
			  "`memberScore` CHAR(40) DEFAULT NULL, " +
			  "`rank` CHAR(40) DEFAULT NULL, " +
			  "`synopsis` BLOB DEFAULT NULL, " +
			  "PRIMARY KEY (`id`)" +
			")");
			db.execSQL("INSERT INTO animeList_backup SELECT id,title,type,imageUrl,episodes,status,watchedEpisodes,score,watchedStatus,dirty,memberScore,rank,synopsis FROM animeList");
			db.execSQL("DROP TABLE animeList");
			db.execSQL(createAnimeList);
			db.execSQL("INSERT INTO animeList SELECT id,title,type,imageUrl,episodes,status,watchedEpisodes,score,watchedStatus,dirty,memberScore,rank,synopsis FROM animeList_backup");
			db.execSQL("DROP TABLE animeList_backup"); */
		}
	}

}